package com.wr.common.utils;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;
import java.util.List;

@Data
public class BaseResult implements Serializable {
    public static final String RESULT_OK="ok";
    public static final String RESULT_NOT_OK="not_ok";

    public static final String SUCCESS="成功操作";



    private String result;
    private String success;
    private Object data;
    private Cursor cursor;
    private List<Error> errors;


    public static BaseResult ok(Object data){
        return createResult(RESULT_OK,SUCCESS,data,null,null);
    }

    public static BaseResult ok(String message){
        return createResult(RESULT_OK,message,null,null,null);
    }

    public static BaseResult ok(Object data,Cursor cursor){
        return createResult(RESULT_OK,SUCCESS,data,cursor,null);
    }

    public static BaseResult ok(){
        return createResult(RESULT_OK,SUCCESS,null,null,null);
    }

    public static BaseResult notOk(List<Error> errors){
        return createResult(RESULT_NOT_OK,"",null,null,errors);

    }


    /**
     *
     * @param result
     * @param data
     * @param cursor
     * @param errors
     * @return
     */
    private static BaseResult createResult(String result,String success,Object data,Cursor cursor,List<Error> errors){
        BaseResult baseResult = new BaseResult();
        baseResult.setResult(result);
        baseResult.setSuccess(success);
        baseResult.setData(data);
        baseResult.setCursor(cursor);
        baseResult.setErrors(errors);
        return baseResult;
    }


    // 内部类
    @Data
    @NoArgsConstructor
    @AllArgsConstructor
     public static class Cursor{
        private int total;
        private int offset;
        private int limit;
    }

    @Data
    @NoArgsConstructor
    @AllArgsConstructor
    public static class Error{
        private String field;
        private String message;
    }
}
